<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div class="player"></div>
  <button id="replay">replay</button>
  <style>
    .player {
      position: absolute;
      top: 0;
      left: 0;
      width: 50px;
      height: 50px;
      background: red;
    }
  </style>
  <script>
    let prevTime = Date.now();

    class Player {
      constructor(dom, speed = 2) {
        this.dom = dom;
        this.speed = speed;
        this.reset();
      }
      reset() {
        this.dom.style.left = 0;
        this.dom.style.top = 0;
      }
      moveLeft() {
        const left = parseInt(this.dom.style.left);
        console.log(left);
        this.dom.style.left = left - this.speed + 'px';
      }
      moveRight() {
        const left = parseInt(this.dom.style.left);
        this.dom.style.left = left + this.speed + 'px';
      }
      moveUp() {
        const top = parseInt(this.dom.style.top);
        this.dom.style.top = top - this.speed + 'px';
      }
      moveDown() {
        const top = parseInt(this.dom.style.top);
        this.dom.style.top = top + this.speed + 'px'
      }
    }
    const player = new Player(document.querySelector('.player'), 10);
    
    const commands = {
      w: 'moveUp',
      a: 'moveLeft',
      s: 'moveDown',
      d: 'moveRight',
    }
    
    const commandStack = [];

    document.addEventListener('keypress', event => {
      const command = player[commands[event.key]].bind(player);
      if(command) {
        command();
        const now = Date.now();
        commandStack.push({
          command,
          timeout: now - prevTime,
        })
        prevTime = now;
      }
    })

    let promise = Promise.resolve();

    replay.addEventListener("click", () => {
      player.reset();
      commandStack.forEach(current => {
        promise = promise.then(() => {
          return new Promise(resolve => {
            setTimeout(() => {
              current.command();
              resolve();
            }, current.timeout)
          })
        })
      })
    })
  </script>
</body>
</html>


